package edu.gyc.game;

import java.util.ArrayList;

/**
 *
 * @author User
 */
public class Game {
    private ArrayList<Ship> ships=new ArrayList<Ship>();
    //用户发射炮弹的次数
    private int count;
    public static void main(String[] args) {
        Game game=new Game();
        game.setUpGame();
        game.startGame();
    }



    private void checkHitCondition(String userInput) {
        count++;
        for(Ship ship:ships){
            String result=ship.checkHit(userInput);
            if(result.equals("hit")){
                System.out.println(ship.getName()+": 被击中");
               // break;
            }else if(result.equals("sink")){
                System.out.println(ship.getName()+ ": 被击沉");
                ships.remove(ship);
                break;//
            }else{
                System.out.println(ship.getName()+": 没打中");
            }


        }
    }

    private void setUpGame() {
        Ship ship1=new Ship("Japan");
        Ship ship2=new Ship("Usa");
        Ship ship3=new Ship("Korea");
        ships.add(ship3);
        ships.add(ship2);
        ships.add(ship1);

        GameHelp.placeShip(ships);
    }

    private void startGame() {
        System.out.println("游戏开始!");
        System.out.println("请用最少的弹药输入击沉三艘战舰");
        while(!ships.isEmpty()){

            String userInput=GameHelp.userInput("请输入两位数字坐标，比如22:");

            checkHitCondition(userInput);
        }
        endGame();

    }

    //游戏结束时，根据count值给用户一个评价 10发以内是神炮手，10-20一般 ，大于20嘲笑用户
    private void endGame() {

        System.out.println("你成功了,一共发射的炮弹是: "+count);
        if (count<=10) {
            System.out.println("神炮手诞生了");
        }else if (count>10&&count<20) {
            System.out.println("小伙子头脑还算聪明!");
        }else{
            System.out.println("哈哈，傻子也比你准！");
        }
    }
}
